package com.wc.blue_bridge_history.Java14Q.A_特殊日期;

/**
 * @Author congge
 * @Date 2024/3/24 15:29
 * @description https://www.lanqiao.cn/paper/3874/problem/2408/
 */
public class Main {
    static int[] m = new int[]{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static void main(String[] args) {
        int sY = 1900, sM = 1, sD = 1;
        int eY = 9999, eM = 12, eD = 31;
        int st = getX(sY, sM, sD);
        int ed = getX(eY, eM, eD);
        int res = 0;
        if (isRun(1900)) {
            m[2] = 29;
        } else m[2] = 28;
        while (st <= ed) {
            if (getS(st % 10000) == getS(st / 10000))
                res++;
            st = nextDay(st);
        }
        System.out.println(res);
    }

    static int getS(int y) {
        int s = 0;
        while (y > 0) {
            s += y % 10;
            y /= 10;
        }
        return s;
    }

    static int nextDay(int ymd) {
        int y = ymd / 10000;
        int mth = ymd % 10000 / 100;
        int d = ymd % 100;

        if (d < m[mth]) {
            return getX(y, mth, d + 1);
        }
        if (mth < 12) {
            return getX(y, mth + 1, 1);
        }
        if (isRun(y + 1)) {
            m[2] = 29;
        } else m[2] = 28;
        return getX(y + 1, 1, 1);
    }

    static int getX(int y, int mth, int d) {
        return y * 10000 + mth * 100 + d;
    }

    static boolean isRun(int y) {
        return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
    }
}
